<script setup lang="ts">
import { ref } from 'vue'
import { getPublickDataApi } from '@/api/global'
import { onLoad } from '@dcloudio/uni-app'
import { getReservationListApi, postReservationApi, putReservationApi } from '@/api/reservation'
import type { ReservationItem, ReservationRequest } from '@/types/reservation'
import { busListStore } from '@/stores'

const form = ref<ReservationRequest>({
  busId: '',
  resType: '1',
  resNo: '',
  eachNo: '',
})

// 获取列表
const busId = busListStore().busList[0].id as string
const pageResult = ref({
  busId: busId,
})
const reservationList = ref<ReservationItem[]>([])
const getReservationList = async () => {
  const res = await getReservationListApi(pageResult.value)
  reservationList.value = res.data.records
}
const contentText = {
  contentdown: '查看更多',
  contentrefresh: '加载中',
  contentnomore: '没有更多'
}

// 提交信息
const onSubmit = async () => {
  form.value.busId = busId
  await postReservationApi(form.value)
  uni.showToast({ icon: 'success', title: '预约成功'})
  getReservationList()
}

// 码类型选择
const onTypeChange: UniHelper.RadioGroupOnChange = (en) => {
  form.value.resType = en.detail.value
}

// 获取每份数量
const getPublickAgency = async () => {
  const res = await getPublickDataApi('RES_EACH_NO')
  form.value.eachNo = res.data
}

// 打电话
const callPhone = (phone: string) => {
  wx.makePhoneCall({
    phoneNumber: phone,
    success: function () {
      console.log('拨打电话成功')
    },
    fail: function () {
      console.log('拨打电话失败')
    }
  })
}

// 修改状态
const putReservation = async (id: string, type: string) => {
  const obj = {
    id,
    status: type == '0' ? '3' : '2',
  }
  const res = await putReservationApi(obj)
  uni.showToast({ icon: 'success', title: "操作成功" })
  getReservationList()
}

onLoad(() => {
  getPublickAgency()
  getReservationList()
})
</script>

<template>
  <!-- 表单 -->
  <view class="form">
    <!-- <view class="top_info">
      <text>页面说明：此界面上加文字说明，纸质版乐促码的说明及使用方法，以及如当地没有代理时乐促码的获取方法。</text>
    </view> -->
    <!-- 表单内容 -->
    <view class="form-content">
      <view class="form-item">
        <text class="label">条码类型</text>
        <radio-group @change="onTypeChange">
          <label class="radio">
            <radio value="1" color="#27ba9b" :checked="form.resType == '1'" />
            自销码
          </label>
          <!-- <label class="radio">
            <radio value="2" color="#27ba9b" :checked="form.resType == '2'" />
            外销码
          </label> -->
        </radio-group>
      </view>
      <!-- <view class="form-item code_text" v-if="form.resType == '1'">自销码说明文字</view> -->
      <!-- <view class="form-item code_text" v-if="form.resType == '2'">外销码说明文字</view> -->
      <view class="form-item">
        <text class="label">预约卷数</text>
        <input class="input" type="text" placeholder="请填写" v-model="form.resNo" />
        <text class="code_note">1卷为 {{ form.eachNo }} 个乐促码</text>
      </view>
      <!-- 提交按钮 -->
      <button class="form-button" @tap="onSubmit">提交预约信息</button>
    </view>
  </view>
  <!-- 预约记录 -->
  <view class="list">
    <text class="title">预约记录</text>
    <view class="order_list">
      <view class="order_item" v-for="item in reservationList" :key="item.id">
        <view class="left">
          <view class="num">预约卷数：{{item.resNo}}，每卷：{{item.eachNo}} 个码</view>
          <view class="type">条码类型：{{item.resType == '1'?'自销码':'外销码'}}</view>
          <view class="time">预约时间：{{ item.createTime }}</view>
        </view>
        <view class="right">
          <view class="status">
            <uni-tag
              inverted
              circle
              :text="
                item.status == '0'
                  ? '未接单'
                  : item.status == '1'
                  ? '已接单'
                  : item.status == '2'
                  ? '已完成'
                  : '已取消'
              "
              :type="
                item.status == '0'
                  ? 'primary'
                  : item.status == '1'
                  ? 'warning'
                  : item.status == '2'
                  ? 'success'
                  : 'default'
              "
            />
            <view class="tle" v-if="item.agentPhone" @tap="callPhone(item.agentPhone)">拨打电话</view>
          </view>

          <view class="btn" v-if="item.status == '0'"><button size="mini" type="warn" @tap="putReservation(item.id,'0')">取消</button></view>
          <view class="btn" v-if="item.status == '1'"><button size="mini" type="primary" @tap="putReservation(item.id,'1')">已收货</button></view>
        </view>
      </view>
    </view>
    <!-- <uni-load-more status="more" :content-text="contentText" /> -->
  </view>
</template>

<style lang="scss">
.top_info {
  font-size: 14px;
  background-color: #fff;
  color: #333;
  padding: 10px 15px;
  line-height: 1.5em;
}
.title {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding-left: 15px;
  height: 40px;
  background-color: #27ba9b;
  font-weight: normal;
  color: #fff;
  font-size: 15px;
  font-weight: bold;
}

.order_list {
  padding: 10px;
  .order_item {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 10px 0;
    border-bottom: 1px solid #eee;
    font-size: 24rpx;
    line-height: 2em;
    color: #666;
    .left {
      .num {
        font-size: 28rpx;
        color: #333;
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-end;
      .tle {
        background-color: #27ba9b;
        color: #fff;
        padding: 2px 5px;
        border-radius: 5px;
        margin-top: 10px;
      }
    }
    .btn {
      overflow: hidden;
      margin-top: 10px;
    }
  }
}

// 表单
.form {
  height: auto;
  padding-bottom: 10px;
  .form-content {
    overflow: hidden;
  }
}
</style>
